const { ipcRenderer } = window.require('electron');
function commonEvent(name, hooks, value) {
  try {
    ipcRenderer.send(name, value);
    ipcRenderer.removeAllListeners(name + '-replay');
    ipcRenderer.on(name + '-replay', function(event, arg) {
      hooks(arg);
    });
  } catch (error) {
    ipcRenderer.removeAllListeners(name + '-replay');
    throw error;
  }
}

function splitMoney(value, length) {
  let splitValue = [];
  if (value) {
    splitValue = parseInt(parseFloat(value) * 100)
      .toString()
      .split('');
  }

  const notSplitLength = length - splitValue.length;
  if (splitValue.length < length) {
    for (var i = 0; i < notSplitLength; i++) {
      splitValue.unshift('　');
    }
  }
  return splitValue;
}

function getsplitSpan(value, length, styles) {
  let splitValue = splitMoney(value, length);
  return splitValue.map((value, index) => {
    if (index === 0 && splitValue.length == 11) {
      return <span key={'v' + index}>{value}</span>;
    } else if (index === 0) {
      return (
        <span key={'v' + index} className={styles.spanfirst}>
          {value}
        </span>
      );
    } else if (index + 1 == splitValue.length) {
      return (
        <span key={'v' + index} className={styles.spanlast}>
          {value}
        </span>
      );
    } else {
      return <span key={'v' + index}>{value}</span>;
    }
  });
}

export default { commonEvent, splitMoney, getsplitSpan };
